package com.webapp.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.model.Circle;
import com.webapp.model.CircleModel;
import com.webapp.model.Person;
import com.webapp.model.User;

/**
 * Servlet implementation class ViewCircle
 */
public class ViewCircle extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewCircle() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// create circle model
		CircleModel cm = new CircleModel();
		
		// get session
		HttpSession session = request.getSession();
		
		// get the person object from session
		Person person = (Person)session.getAttribute("UserObject");
		String x = person.getType().toLowerCase();
		User myUser = myUser = (User) person;
		
		// get the circle to view
		int circleID = Integer.parseInt((String)request.getParameter("fenceIDTxt"));
		System.out.println("circle id: " + circleID);
		Circle theCircle = cm.getCircle(circleID);
		
		//get a list of users in a circle
		List<Integer> listOfFriends = cm.getUserIdList(circleID);
		
		//return the circle and a list of friends
		session.setAttribute("Friends", listOfFriends);
		session.setAttribute("Circle", theCircle);
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ViewFriends.jsp");
		dispatcher.forward(request, response);	
		
		
	}
}
